package crayon.ui;
import crayon.fc.*;
import javax.swing.JSplitPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.AbstractAction;
import javax.swing.JToolBar;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.KeyStroke;
import java.awt.event.ActionListener;
public class Main extends JFrame  {
    final GestionnaireTracing gestionnaireTracing;
    final Grapher grapher;
    Interaction m_interaction;
    JToolBar toolbar;
    
    
    Main(String title, String[] expressions) {
        super(title);
	setDefaultCloseOperation(EXIT_ON_CLOSE);
	gestionnaireTracing = new GestionnaireTracing();
	grapher = new Grapher(gestionnaireTracing);
	for(String expression : expressions) {
            System.out.println(expression);
        }
	/** Definition des interactions a la souris **/
	m_interaction=new Interaction(grapher,gestionnaireTracing);        
        grapher.addMouseWheelListener(m_interaction);
	grapher.addMouseListener(m_interaction);
	grapher.addMouseMotionListener(m_interaction);
	this.addKeyListener(m_interaction);
	add(grapher);
        setJMenuBar(createMenuBar()); 
        pack();
    }
    
    public void update(){
	grapher.update();
    }
    
    public static void main(String[] argv) {
        final String[] expressions = argv;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Main("grapher", expressions).setVisible(true);
            }
        });
    }

   
    
    
    private JMenuBar createMenuBar(){
        JMenuBar menuBar= new JMenuBar();
        JMenu  menu = new JMenu("Edit");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription("Menu d'edition");
        menuBar.add(menu);
        JMenuItem menuItem = new JMenuItem("clear",KeyEvent.VK_T);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
	menuItem.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
		    gestionnaireTracing.vider();
		    grapher.update();
		}	
	    }); 
        menuItem.getAccessibleContext().setAccessibleDescription("clear");
	// ADD LISTENER
        menu.add(menuItem);
	return menuBar;
       
    }
    
    

}